import 'package:flutter/material.dart';
import 'package:lib_ui/gen/colors.gen.dart';
import 'package:lib_ui/widget/main_button.dart';
import '../../common/button_action.dart';

class MsgDialog extends StatelessWidget {
  final Widget? title;
  final Widget? content;
  final ButtonAction? negative;
  final ButtonAction? positive;

  const MsgDialog({
    Key? key,
    this.title,
    this.content,
    this.negative,
    this.positive,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget? titleWidget;
    Widget? contentWidget;
    Widget? actions;

    if (title != null) {
      titleWidget = Container(
        margin: EdgeInsets.only(top: content == null ? 16 : 0),
        alignment: Alignment.center,
        child: DefaultTextStyle(
          style: const TextStyle(fontSize: 16.5, color: ColorName.textPrimary),
          textAlign: TextAlign.center,
          child: title!,
        ),
      );
    }

    if (content != null) {
      contentWidget = Container(
        margin: const EdgeInsets.only(top: 4.5),
        alignment: Alignment.center,
        child: DefaultTextStyle(
          style: const TextStyle(fontSize: 13.5, color: ColorName.textHint),
          textAlign: TextAlign.center,
          child: content!,
        ),
      );
    }

    if (negative != null || positive != null) {
      Widget? negativeBtn;
      Widget? positiveBtn;

      if (negative != null) {
        negativeBtn = Expanded(
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: positive == null ? 25 : 0),
            child: MainButton(
              text: negative!.text ?? '取消',
              type: negative!.type ?? MainButtonType.normal,
              onClick: negative!.onClick,
            ),
          ),
        );
      }

      if (positive != null) {
        positiveBtn = Expanded(
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: negative == null ? 25 : 0),
            child: MainButton(
              text: positive!.text ?? '确定',
              type: positive!.type ?? MainButtonType.primary,
              onClick: positive!.onClick,
            ),
          ),
        );
      }

      actions = Container(
        margin: EdgeInsets.only(top: content == null ? 32 : 15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            if (negative != null) negativeBtn!,
            if (negative != null && positive != null) Container(width: 9),
            if (positive != null) positiveBtn!,
          ],
        ),
      );
    }

    final columnChildren = <Widget>[
      if (title != null) titleWidget!,
      if (content != null) contentWidget!,
      if (negative != null || positive != null) actions!,
    ];

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: columnChildren,
    );

    return Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(21)),
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 24, horizontal: 25),
        child: dialogChild,
      ),
    );
  }
}